package com.collect.zambia.zhiScene.zhiLogin.zhiFace;

import android.content.Context;

import com.collect.zambia.zhiBase.BasePresenter;
import com.collect.zambia.zhiBase.BaseSubscriber;
import com.collect.zambia.zhiBean.zhiSend.ZHISFaceEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISendEntity;
import com.collect.zambia.zhiNetApi.ZHIHttpApiProvider;
import com.collect.zambia.zhiNetApi.ZHIHttpResponse;
import com.collect.zambia.zhiUtils.RxSchedulers;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ZHIFacePresenter extends BasePresenter<ZHIFaceContract.View> implements ZHIFaceContract.Presenter {
    @Inject
    public ZHIFacePresenter() {

    }

    @Override
    public void uploadImageInfo(Context context, String filePath) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            Map<String, String> tokenMap = (Map<String, String>) object;
                            String url = tokenMap.get("url");
                            mView.onUploadImageInfo(url);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("uploadImageInfo");
                    }
                };

        Map<String, String> map = new HashMap<>();

        File file = new File(filePath);
        //判断media类型
        String mediaType = "image/jpg";
        if (filePath.endsWith(".png")) {
            mediaType = "image/png";
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse(mediaType), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestBody);

        ZHIHttpApiProvider.getApi().uploadImage(map, body)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void saveFaceImageInfo(Context context, ZHISFaceEntity zhisFaceEntity) {
        ZHISendEntity zhiSendEntity = new ZHISendEntity();
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onSaveFaceImageInfo();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        try {
                            showMessage(messge);
                            mView.onGetErrorInfo("saveFaceImageInfo");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
        ZHIHttpApiProvider.getApi().saveFaceImageInfo(zhisFaceEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }
}
